import { defineStore } from 'pinia'


export const useLoaderStorage = defineStore('loader', {
  state: () => {
    return {
      count: 0,
      currentQuantity: 0,
    }
  },
  getters: {
    isLoading: (state) => {
      // return state.currentQuantity > 0 || state.count === 0
      return state.currentQuantity < state.count || state.count === 0
    },
    progress: (state) => {
      return state.count === 0 ? '' : `${state.currentQuantity} / ${state.count}`
    },
  },
  actions: {
    async loader(fnList: Function[] | Function){
      if (!Array.isArray(fnList)) {
        fnList = [fnList]
      }
      if (fnList.length === 0) return

      this.count += fnList.length
      // this.currentQuantity += fnList.length

      for(const fn of fnList) {
        await fn()
        this.loaderEnd()
      }
    },
    loaderEnd(){
      this.currentQuantity += 1
    },
    reset() {
      this.count = this.currentQuantity = 0
    },
  }
})
